package io.renren.modules.archive.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import io.renren.modules.archive.dao.DictionaryDao;
import io.renren.modules.archive.entity.ArchiveAttachEntity;
import io.renren.modules.archive.entity.DictionaryEntity;
import io.renren.modules.archive.service.DictionaryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import javax.xml.ws.Action;


@Service("dictionaryService")
public class DictionaryServiceImpl extends ServiceImpl<DictionaryDao, DictionaryEntity> implements DictionaryService {

    @Autowired
    private DictionaryDao dictionaryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DictionaryEntity> page = this.page(
                new Query<DictionaryEntity>().getPage(params),
                new QueryWrapper<DictionaryEntity>()
                        .eq(io.renren.common.utils.StringUtils.isNotBlank((String) params.get("key")),"DICTIONARY_TYPE",(String) params.get("key"))
        );

        return new PageUtils(page);
    }

    @Override
    public List<DictionaryEntity> queryAll(Map<String, Object> params) {
        String dictionaryType = (String)params.get("dictionaryType");
        QueryChainWrapper<DictionaryEntity> wrapper = new QueryChainWrapper<>(dictionaryDao);
        wrapper.eq(StringUtils.isNotBlank(dictionaryType),"dictionary_type", dictionaryType)
                .orderByAsc("dictionary_order");
        List<DictionaryEntity> dictList= wrapper.list();
        return dictList;
    }

    @Override
    public List<DictionaryEntity> getDictDataListByType(String dictionaryType){
        QueryChainWrapper<DictionaryEntity> wrapper = new QueryChainWrapper<>(dictionaryDao);
        wrapper.eq(StringUtils.isNotBlank(dictionaryType),"dictionary_type", dictionaryType)
                .orderByAsc("dictionary_order");
        List<DictionaryEntity> dictList= wrapper.list();

        return dictList;


    }

    @Override
    public List<DictionaryEntity> getDictDataListByTypes(String dictionaryTypes){
        QueryChainWrapper<DictionaryEntity> wrapper = new QueryChainWrapper<>(dictionaryDao);
        List<String> dictionaryTypesList = Arrays.asList(dictionaryTypes.split(","));
        wrapper.in(StringUtils.isNotBlank(dictionaryTypes),"dictionary_type",dictionaryTypesList)
                .orderByAsc("dictionary_type").orderByAsc("dictionary_order");
        List<DictionaryEntity> dictList= wrapper.list();

        return dictList;


    }







    @Override
    public String queryBydictionaryName(String dictionaryName,String dictionaryType) {

        QueryChainWrapper<DictionaryEntity> wrapper = new QueryChainWrapper<>(dictionaryDao);
        wrapper.select("dictionary_value")
                .eq(StringUtils.isNotBlank(dictionaryType),"dictionary_type", dictionaryType)
                .eq(StringUtils.isNotBlank(dictionaryName),"dictionary_name", dictionaryName)
                .orderByAsc("dictionary_order");
        String dictionaryValue= wrapper.list().get(0).getDictionaryValue();

        return dictionaryValue;
    }

    @Override
    public String queryBydictionaryValue(String dictionaryValue,String dictionaryType) {

        QueryChainWrapper<DictionaryEntity> wrapper = new QueryChainWrapper<>(dictionaryDao);
        wrapper.select("dictionary_name")
                .eq(StringUtils.isNotBlank(dictionaryType),"dictionary_type", dictionaryType)
                .eq(StringUtils.isNotBlank(dictionaryValue),"dictionary_value", dictionaryValue)
                .orderByAsc("dictionary_order");
        List<DictionaryEntity> dictList=wrapper.list();
        String dictionaryName=dictList.get(0).getDictionaryName();

        return dictionaryName;
    }
}